<?php

class banners extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);		
	}
	
	public function __default () {
		throw new NotFound;
	}
	
	public function get ($pageType, $position) {
		$typesController = TypesController::getInstance ();
		
		$response = new ModuleResponse;
		
		$oTypeBanner = $typesController->getTypeByName('banner');
		$oTypePage = $typesController->getTypeByName('banners_page_type');
		$oTypePosition = $typesController->getTypeByName('banner_position');
		
		$selType = new Selection;
		$selType->addType($oTypePage);
		$selType->addFilter('alias', $pageType);
		$selType->addLimit(1);
		
		$res = $selType->getList();
		
		if (count($res) == 0) {
			return $response;
		}
		
		$pageTypeId = $res[0][0];
		
		$selPos = new Selection;
		$selPos->addType($oTypePosition);
		$selPos->addFilter('alias', $position);
		$selPos->addLimit(1);
		
		$res = $selPos->getList();
		
		if (count($res) == 0) {
			return $response;
		}
		
		$positionId = $res[0][0];
		
		$sel = new Selection;
		$sel->addType($oTypeBanner);
		$sel->addFilter('pages',$pageTypeId);
		$sel->addFilter('position',$positionId);
		$sel->addFilter('active', '1');
		$sel->addFilter('date_start', time(), '<');
		$sel->addFilter('date_end', time(), '>');
				
		$sel->setOrder('weight');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		foreach ($aObjects as $object) {
			$object->disableHTMLFilter('content');
			$response->addObject($object);
		}
		
		return $response;
	}
	
}